package com.wt.二分法;

import java.util.Arrays;

public class dichoutomy {

    public int two(int arr[],int low,int high,int key) {
        Arrays.sort(arr);
        if (key < arr[low] || key > arr[high] || low > high) {
            return -1;
        }
        int middle = (low + high) / 2;
        if (key > arr[middle]) {
            return two(arr, middle + 1, high, key);
        }else if(key < arr[middle]){
            return two(arr, low, middle - 1, key);
        }else{
            return  middle;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 7, 5, 6, 4, 2, 8, 9, 3, 10, 13, 12};
        dichoutomy dichoutomy = new dichoutomy();
        int two = dichoutomy.two(arr, 0, arr.length - 1, 13);
        System.out.println("值是:"+two);
    }
}
